
import { SyncStorage } from "./SyncStorage";
import {http} from "./fetch";





export const request = (url, data = {}, method = "GET") => {
  return http({
    url: url,
    data: data,
    method: method,
  });
};
export const requestByUserId = async (url, data = {userId:null}, method = "GET") => {
  if (!data.userId) {
    return { code: -1, message: "缺少userId" };
  }
  return http({
    url: url,
    data: data,
    method: method,
  });
};

function isJSONStr(str) {
  if (typeof str == "string") {
    return true;
  }
  return false;
}

// const ip = ['https://searchplugin.csdn.net/api/v1/ip/get']
let ip = ["/v1/ip/get", "/getIP"];
const iphttp = [
  "https://searchplugin.csdn.net/api/v1/ip/get",
  "https://www.ipplus360.com/getIP",
];
export function getIpAddr(index = 0) {
  // if (Taro.ENV_TYPE.WEAPP === Taro.getEnv()) {
  //   ip = iphttp;
  // }
  return qureyJson(ip[index]).then((response) => {
    const res = response.data;

    if (response.statusCode != 500) {
      if (res.ip) {
        SyncStorage.setStorageSync("ip", res.ip);
        const address = res.address.split(" ");
        SyncStorage.setStorageSync("address", address[0] + address[1] + address[2]);
      } else if (res.data.ip) {
        SyncStorage.setStorageSync("ip", res.data.ip);
        const address = res.data.address.split(" ");
        SyncStorage.setStorageSync("address", address[0] + address[1] + address[2]);
      } else if (res.data) {
        SyncStorage.setStorageSync("ip", res.data);
      }
      console.log("test", SyncStorage.getValue("ip"), index);
      // info(response.statusCode + '')
    } else {
      if (index + 1 < ip.length) {
        getIpAddr(index + 1);
      }
    }
  });
}

function qureyJson(url) {
  return fetch(url,{
    method: "GET",
  }).then((res) => {
    return res;
  });
}

export function sumtime(seconds) {
  let dd;
  let hh;
  let mm;
  let ss;
  if (seconds === null || seconds < 0) return;
  let pseconds = parseInt(seconds, 10);
  // // 得到天
  // dd = (pseconds / (3600 * 24)) | 0
  // pseconds = parseInt(pseconds + '', 10) - parseInt(dd + '', 10) * 3600 * 24
  // 得到小时
  hh = (pseconds / 3600) | 0;
  if (parseInt(hh + "", 10) < 10) {
    hh = `0${hh}`;
  }
  // if (parseInt(hh + '', 10) >= 24) {
  //   hh = '00'
  // }
  pseconds = parseInt(pseconds + "", 10) - parseInt(hh + "", 10) * 3600;
  // 得到分钟
  mm = (parseInt(pseconds + "", 10) / 60) | 0;

  if (parseInt(mm + "", 10) < 10) {
    mm = `0${mm}`;
  }

  // 得到秒
  ss = parseInt(pseconds + "", 10) - parseInt(mm + "", 10) * 60;
  if (parseInt(ss + "", 10) < 10) {
    ss = `0${ss}`;
  }

  return `${hh}:${mm}:${ss}`;
}

/**
 *
 * @param num
 * @param isInput 是否输入框
 * @param showPoint 是否显示小数点
 * @returns
 */
export const digitalFormat = (num, isInput = false, showPoint = true) => {
  if (num == "" && isInput) return "";
  const _lessZero = Number(num) < 0 ? true : false;
  let str = num == "-" ? "0.00" : num?.toString().replace("-", "") || "0.00";
  let newStr = "";
  let count = 0;

  if (str.indexOf(".") == -1) {
    for (let i = str.length - 1; i >= 0; i--) {
      if (typeof Number(str[i]) != "number") {
        count++;
        continue;
      }

      if (count % 3 == 0 && count != 0) {
        newStr = str.charAt(i) + "," + newStr;
      } else {
        newStr = str.charAt(i) + newStr;
      }
      count++;
    }
    if (showPoint) {
      str = newStr + ".00"; //自动补小数点后两位
    } else {
      str = newStr;
    }
  } else {
    for (let i = str.indexOf(".") - 1; i >= 0; i--) {
      if (typeof Number(str[i]) != "number") {
        count++;
        continue;
      }

      if (count % 3 == 0 && count != 0) {
        newStr = str.charAt(i) + "," + newStr;
      } else {
        newStr = str.charAt(i) + newStr; //逐个字符相接起来
      }
      count++;
    }
    str = newStr + (str + "00").substr((str + "00").indexOf("."), 3);
  }
  return _lessZero ? "-" + str : str;
};

export const getUUID = () => {
  // return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => {
  return "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, (c) => {
    const r = (Math.random() * 16) | 0;
    const v = c === "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
};


export const getFormatTime=(time)=>{
  return time.replace(/T/g, ' ').replace(/.[\d]{3}Z/, ' ')
}
export default {
 
  request,
  requestByUserId,
  
  getIpAddr,
  getUUID,

};
